package com.aizhixin.lab.course.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.course.domain.CourseChapterDomain;
import com.aizhixin.lab.course.course.domain.CourseChapterNameDomain;
import com.aizhixin.lab.course.course.dto.*;
import com.aizhixin.lab.course.course.entity.Course;
import com.aizhixin.lab.course.course.entity.CourseChapter;
import com.aizhixin.lab.course.course.entity.CourseChapterFile;
import com.aizhixin.lab.course.course.service.TeacherChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/course/teacher/chapter")
@Api(value = "课程章节相关API")
public class TeacherChapterController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private TeacherChapterService chapterService;

    @RequestMapping(value = "/initChapterCount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "初始化章节统计数量", response = Void.class, notes = "初始化章节统计数量<br>@author hsh")
    public ResponseEntity<?> initChapterCount(@RequestHeader("Authorization") String token) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
        Map<String, Object> result = chapterService.initChapterCount();
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节", response = Void.class, notes = "查询课程章节<br>@author hsh")
    public ResponseEntity<?> getList(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapter> result = chapterService.getList(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getChapter", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "根据章节id查询章节和课程名称", response = Void.class, notes = "根据章节id查询章节和课程名称<br>@author hsh")
    public ResponseEntity<?> getChapter(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "chapterId", required = false) @RequestParam(value = "chapterId", required = false) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        CourseChapterNameDomain result = chapterService.getChapterName(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getListWithFile", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节包含文件,包含隐藏文件,预览时用", response = Void.class, notes = "查询课程章节包含文件,包含隐藏文件,预览时用<br>@author hsh")
    public ResponseEntity<?> getListWithFile(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

        List<CourseChapterDomain> result = chapterService.getListWithFile(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getListWithFileWithoutHide", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节包含文件, 不包含隐藏文件, 授课时用", response = Void.class, notes = "查询课程章节包含文件,不包含隐藏文件,授课时用<br>@author hsh")
    public ResponseEntity<?> getListWithFileWithoutHide(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapterDomain> result = chapterService.getListWithFileWithoutHide(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "添加课程章节", response = Void.class, notes = "添加课程章节<br>@author hsh")
    public ResponseEntity<?> add(@RequestHeader("Authorization") String token,
                                 @ApiParam(value = "chapter type 10:章 20:节", required = false) @RequestBody CourseChapterAddDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.add(dto,true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存课程章节", response = Void.class, notes = "保存课程章节<br>@author hsh")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "chapter", required = false) @RequestBody CourseChapterDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.save(dto,true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveSort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存课程章节排序", response = Void.class, notes = "保存课程章节排序<br>@author hsh")
    public ResponseEntity<?> saveSort(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "chapter", required = false) @RequestBody CourseChapterSortDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.saveSort(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除课程章节", response = Void.class, notes = "删除课程章节<br>@author hsh")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "chapterId", required = true) @RequestParam(value = "chapterId", required = true) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.delete(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getFileList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节文件,编辑时用", response = Void.class, notes = "查询课程章节文件,编辑时用<br>@author hsh")
    public ResponseEntity<?> getFileList(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "chapterId", required = true) @RequestParam(value = "chapterId", required = true) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapterFile> result = chapterService.getFileList(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getFileListWithoutHide", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节文件,预览时用", response = Void.class, notes = "查询课程章节文件,预览时用<br>@author hsh")
    public ResponseEntity<?> getFileListWithoutHide(@RequestHeader("Authorization") String token,
                                                    @ApiParam(value = "chapterId", required = true) @RequestParam(value = "chapterId", required = true) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapterFile> result = chapterService.getFileListWithoutHide(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/hide", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "章节隐藏", response = Void.class, notes = "章节隐藏<br>@author hsh")
    public ResponseEntity<?> hide(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "chapterId", required = true) @RequestParam(value = "chapterId", required = true) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.hide(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存文件", response = Void.class, notes = "保存文件<br>@author hsh")
    public ResponseEntity<?> saveFile(@RequestHeader("Authorization") String token,
                                      @ApiParam(value = "type 10:视频 20:课件 30:老师参考 40:实验手册", required = false) @RequestBody CourseChapterFileDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result=new HashMap<>();
        if (StringUtils.isEmpty(dto.getChapterId())){
            result.put(ApiReturnConstants.SUCCESS,false);
            result.put(ApiReturnConstants.ERROR,"未选择章节");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        result = chapterService.saveFile(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveFileOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存文件顺序", response = Void.class, notes = "保存文件顺序<br>@author hsh")
    public ResponseEntity<?> saveFileOrder(@RequestHeader("Authorization") String token,
                                           @ApiParam(value = "list", required = false) @RequestBody CourseChapterFileOrderListDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.saveFileOrder(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/hideFile", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "文件隐藏", response = Void.class, notes = "文件隐藏<br>@author hsh")
    public ResponseEntity<?> hideFile(@RequestHeader("Authorization") String token,
                                      @ApiParam(value = "fileId", required = true) @RequestParam(value = "fileId", required = true) Long fileId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.hideFile(fileId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/deleteFile", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除课程章节文件", response = Void.class, notes = "删除课程章节文件<br>@author hsh")
    public ResponseEntity<?> deleteFile(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "fileId", required = true) @RequestParam(value = "fileId", required = true) Long fileId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.deleteFile(fileId);
        return new ResponseEntity(result, HttpStatus.OK);
    }


//    @RequestMapping(value = "/att/getNoteList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(httpMethod = "GET", value = "查询课程章节课堂笔记", response = Void.class, notes = "查询课程章节课堂笔记<br>@author hsh")
//    public ResponseEntity<?> getNoteList(@RequestHeader("Authorization") String token,
//                                         @ApiParam(value = "chapterId", required = true) @RequestParam(value = "chapterId", required = true) Long chapterId) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
//        List<CourseChapterAtt> result = chapterService.getAttList(chapterId, 10);
//        return new ResponseEntity(result, HttpStatus.OK);
//    }
//
//    @RequestMapping(value = "/att/getVideoList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(httpMethod = "GET", value = "查询课程章节课堂视频", response = Void.class, notes = "查询课程章节课堂视频<br>@author hsh")
//    public ResponseEntity<?> getVideoList(@RequestHeader("Authorization") String token,
//                                          @ApiParam(value = "chapterId", required = true) @RequestParam(value = "chapterId", required = true) Long chapterId) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
//        List<CourseChapterAtt> result = chapterService.getAttList(chapterId, 20);
//        return new ResponseEntity(result, HttpStatus.OK);
//    }
//
//    @RequestMapping(value = "/att/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(httpMethod = "POST", value = "保存课堂笔记和课堂视频", response = Void.class, notes = "保存课堂笔记和课堂视频 type:10:课堂笔记 20:课堂视频<br>@author hsh")
//    public ResponseEntity<?> saveAtt(@RequestHeader("Authorization") String token,
//                                     @ApiParam(value = "chapter type:10:课堂笔记 20:课堂视频", required = false) @RequestBody CourseChapterAttDTO dto) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
//        Map<String, Object> result = chapterService.saveAtt(dto);
//        return new ResponseEntity(result, HttpStatus.OK);
//    }
//
//    @RequestMapping(value = "/att/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(httpMethod = "DELETE", value = "删除课程章节", response = Void.class, notes = "删除课程章节<br>@author hsh")
//    public ResponseEntity<?> deleteAtt(@RequestHeader("Authorization") String token,
//                                       @ApiParam(value = "attId", required = true) @RequestParam(value = "attId", required = true) Long attId) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
//        Map<String, Object> result = chapterService.deleteAtt(attId);
//        return new ResponseEntity(result, HttpStatus.OK);
//    }
}
